package com.rao.quartz.controller;

import com.rao.quartz.service.OssUploadService;
import com.rao.quartz.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: raoguang
 * @Date: 2022/08/21/20:13
 * @Description:
 */
@Controller
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private OssUploadService ossUploadService;

    /**
     * 文件上传具体实现
     *
     * @param multipartFile
     * @param request
     * @return
     */
    @RequestMapping("/uploadoss/file")
    @ResponseBody
    public String uploadOss(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {

        if (multipartFile.isEmpty()) {
            return null;
        }
        long size = multipartFile.getSize();
        String originalFileName = multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();

        //获取用户指定的文件夹，问这个文件夹为什么要从页面上传递过来呢？
        //原因：做隔离，不同业务，不同文件放在不同的目录中。
        String dir = request.getParameter("dir");
        return ossUploadService.upLoadFile(multipartFile, dir);

    }

    /**
     * 文件上传具体实现
     *
     * @param multipartFile
     * @param request
     * @return
     */
    @RequestMapping("/upload/file")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {

        if (multipartFile.isEmpty()) {
            return null;
        }
        long size = multipartFile.getSize();
        String originalFileName = multipartFile.getOriginalFilename();
        String contentType = multipartFile.getContentType();

        //获取用户指定的文件夹，问这个文件夹为什么要从页面上传递过来呢？
        //原因：做隔离，不同业务，不同文件放在不同的目录中。
        String dir = request.getParameter("dir");
        return uploadService.upLoadImg(multipartFile, dir);

    }
}
